"""
简化版的MarkItDown - 临时解决方案
避免网络依赖问题，提供基本的文档转换功能
"""

import os
from typing import Optional, Union, IO

class DocumentConverterResult:
    """文档转换结果"""
    def __init__(self, title: str = "", text_content: str = ""):
        self.title = title
        self.text_content = text_content

class MarkItDownSimple:
    """简化版的MarkItDown，避免复杂依赖"""
    
    def __init__(self):
        pass
    
    def convert(self, source: Union[str, bytes, IO], **kwargs) -> DocumentConverterResult:
        """
        转换文档到Markdown格式
        
        Args:
            source: 文件路径、字节内容或文件对象
            **kwargs: 其他参数
            
        Returns:
            DocumentConverterResult: 转换结果
        """
        if isinstance(source, str):
            if os.path.isfile(source):
                return self._convert_file(source, **kwargs)
            else:
                # 当作文本内容处理
                return DocumentConverterResult(
                    title="Text Content",
                    text_content=source
                )
        elif isinstance(source, bytes):
            return self._convert_bytes(source, **kwargs)
        else:
            # IO对象
            return self._convert_stream(source, **kwargs)
    
    def _convert_file(self, file_path: str, **kwargs) -> DocumentConverterResult:
        """转换文件"""
        filename = os.path.basename(file_path)
        title = os.path.splitext(filename)[0]
        
        try:
            # 尝试读取为文本文件
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                content = f.read()
            
            return DocumentConverterResult(
                title=title,
                text_content=content
            )
        except Exception as e:
            return DocumentConverterResult(
                title=title,
                text_content=f"无法读取文件: {str(e)}"
            )
    
    def _convert_bytes(self, content: bytes, **kwargs) -> DocumentConverterResult:
        """转换字节内容"""
        try:
            text_content = content.decode('utf-8', errors='ignore')
            return DocumentConverterResult(
                title="Byte Content",
                text_content=text_content
            )
        except Exception as e:
            return DocumentConverterResult(
                title="Byte Content",
                text_content=f"无法解码内容: {str(e)}"
            )
    
    def _convert_stream(self, stream: IO, **kwargs) -> DocumentConverterResult:
        """转换IO流"""
        try:
            content = stream.read()
            if isinstance(content, bytes):
                return self._convert_bytes(content, **kwargs)
            else:
                return DocumentConverterResult(
                    title="Stream Content",
                    text_content=str(content)
                )
        except Exception as e:
            return DocumentConverterResult(
                title="Stream Content", 
                text_content=f"无法读取流: {str(e)}"
            )

# 创建别名以保持兼容性
MarkItDown = MarkItDownSimple
